package com.shibo.generator.service;

import com.shibo.generator.entity.TableInfo;
import com.shibo.generator.temUtils.TemCommon;
import com.shibo.generator.temUtils.TemplateEnum;
import com.shibo.generator.util.Constant;
import com.shibo.generator.util.DataTypeEnums;
import com.shibo.generator.util.IoUtils;
import com.shibo.generator.util.ReplaceUtlis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/*
 * 根据模板生成模板数据
 * 主要思路 模板替换
 * */
@Service
public class TemplateExtendGenerator {

    @Autowired
    private TemCommon temCommon;


    @Autowired
    private TableInfoService tableInfoService;

    /*
     * 生成实体类
     * */
    public void generateEntity(String tableName) {
        //生成模板的数据
        Map<String, Object> replaceMap = temCommon.replaceMap(tableName, "code generator entity");
        replaceMap.put("entityData", returnEntityCols(tableName));
        replaceMap.put("package", temCommon.getPackagePath(Constant.entity));
        replaceMap.put("tableName", tableName);
        //写入指定的路径
        generateFile(Constant.entity, replaceMap.get("className").toString(), replaceMap);
    }

    //基于mybatis plus生成mapper
    public void generateMapper(String tableName) {
        //生成模板的数据
        Map<String, Object> replaceMap = temCommon.replaceMap(tableName, "code generator mapper");
        replaceMap.put("entityAddress",temCommon.getPackagePath(Constant.entity)+"."+replaceMap.get("className").toString());
        replaceMap.put("package", temCommon.getPackagePath(Constant.mapper));
        //写入指定的路径
        generateFile(Constant.mapper, replaceMap.get("className").toString() + "Mapper", replaceMap);
    }

    //生成service(controller)暴漏对外接口  基于table生成服务
    public void generateService(String tableName) {
        //生成模板的数据
        Map<String, Object> replaceMap = temCommon.replaceMap(tableName, "code generator service");
        replaceMap.put("entityAddress",temCommon.getPackagePath(Constant.entity)+"."+replaceMap.get("className").toString());
        replaceMap.put("mapperAddress",String.format("%s.%sMapper",temCommon.getPackagePath(Constant.mapper),replaceMap.get("className").toString()));
        //写入指定的路径
        generateFile(Constant.service,replaceMap.get("className").toString() + "Service", replaceMap );
    }

    /*
    * 公共写入方法
    * */
    private void generateFile(String temType, String pathName, Map<String, Object> replaceMap) {
        String tem = new IoUtils().read(temCommon.returnFileName(temType));//根据模板类型读取模板信息
        tem = ReplaceUtlis.replace(tem,replaceMap); //替换后的模板数据
        IoUtils.write(temCommon.getWriteFileBasePath() + TemplateEnum.valueOf(temType).getTemName(), pathName, tem);
    }

    /*
     * 根据表名返回字段
     * */
    public String returnEntityCols(String tableName) {
        StringBuilder cols = new StringBuilder();
        for (TableInfo tableColumn : tableInfoService.getTableColumns(tableName)) {
            cols.append("    private ");
            cols.append(DataTypeEnums.getJavaDataTypeByMysqlDataType(tableColumn.getDataType()));
            cols.append(" ");
            cols.append(temCommon.formatNameLowerCase(tableColumn.getColumnName()));
            cols.append(";\n");
        }
        return cols.toString();
    }

}
